#include "utility.h"

State xState;

void init_state(){
	memset(xState.version.strASCIICompileProfile,0,sizeof(xState.version.strASCIICompileProfile) );
	sprintf(xState.version.strASCIICompileProfile, "Compile time - %s %s", __DATE__, __TIME__);
    xState.cnt_timer_call = 0;
    xState.flag_spi_configuring = 0;
    xState.flag_spi_configure_ok = 0;
    xState.cnt_spi_configure_failed = 0;
    xState.flag_start_timer = 0;
}

void null_xil_printf( const char8 *ctrl1, ...){
	;
}

void gw_sleep(int32_t cnt){
    int i,j;
    for(i=0;i<cnt;i++){
        for(j=0;j<cnt;j++){
            ;
        }
    }
}

void makeEvent(Event* pEvent,int32_t myId,Event_ID evtId, const void* pData){
    pEvent->ID = evtId;
    pEvent->src = myId;
    pEvent->pData = (void*) pData;
}

/* fpga_version */
char* intTimeToString(int number){
	char* temp = (char*)pvPortMalloc(32);
	
	if(number < 10){// 1
		temp[0] = '0';
		temp[1] = '0' + (char)number;
		temp[2] = '\0';
	}else if(number >= 2000){ // 4
		char a[4];
		a[0] = 2;
		a[1] = 0;
		a[2] = (char)((number - 2000)/10);
		a[3] = (char)((number - 2000)%10);
		temp[0] = a[0] + '0';
		temp[1] = a[1] + '0';
		temp[2] = a[2] + '0';
		temp[3] = a[3] + '0';
		temp[4] = '\0';
	}else{ // 2
		temp[0] = (char)(number/10) + '0';
		temp[1] = (char)(number%10) + '0';
		temp[2] = '\0';
	}
	return temp;
}

char* parse_fpga_version(uint32_t number){
	char* version = (char*)pvPortMalloc(32);
	int day,month,year,hours,minutes,seconds;
	char* tmp = NULL;
	seconds = number - ((number>>6)<<6);
	tmp = intTimeToString(seconds);
	memcpy(version + 12,tmp,2);
	vPortFree(tmp);
	tmp = NULL;
	
	number = number >> 6;
	minutes = number -((number>>6)<<6);
	tmp = intTimeToString(minutes);
	memcpy(version + 10,tmp,2);
	vPortFree(tmp);
	tmp = NULL;
	
	number = number >> 6;
	hours = number - ((number>>5)<<5);
	tmp = intTimeToString(hours);
	memcpy(version + 8,tmp,2);
	vPortFree(tmp);
	tmp = NULL;
	
	number = number >> 5;
	year = 2000 + number - ((number>>6)<<6);
	tmp = intTimeToString(year);
	memcpy(version,tmp,4);
	vPortFree(tmp);
	tmp = NULL;
	
	number = number >> 6;
	month = number - ((number>>4)<<4);
	tmp = intTimeToString(month);
	memcpy(version + 4,tmp,2);
	vPortFree(tmp);
	tmp = NULL;
	
	number = number >> 4;
	day = number;
	tmp = intTimeToString(day);
	memcpy(version+6,tmp,2);
	vPortFree(tmp);
	tmp = NULL;
	
	version[14] = '\0';
	
	return version;
}

